<?php

/*
 * auth.php
 *
 * function authenticate($suser,$spass)
 *
 * tries to authenticate against the seesmic api using username and password
 * adds username and SID to session and returns true if works
 * returns false if error
 *
 */

function authenticate($suser,$spass){

    // authenticate against the seesmic api
    $seesmicAuthUrl = "http://api.seesmic.com/login/auth?username=".$suser."&password=".md5($suser.$spass);

    $ch = curl_init($seesmicAuthUrl);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, "seesmicsidebar"); //give your script an identity, so they can see you
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_TIMEOUT, 55);
    $result = curl_exec($ch);
    $resultArray = curl_getinfo($ch); // this can be useful to see http headers in the reply using print_r($resultArray)
    $JSON = json_decode($result);

    if($JSON->login->success=="true"){
        $_SESSION['seesmicName']=$suser;
        $_SESSION['seesmicSid']=$JSON->login->sid;
		curl_close($ch);
		return true;
    } else {
		return false;
    }
}
?>